Return to doc.sitecore.com

Selecting Items in the Master Section of a Master Programmatically
Prev Next

Author: Alexander Tsvirchkov
Posted: 3/14/2006 9:46:10 AM

It is required to select Items in the master section of a master programmatically:

/upload/sdn5/scrapbook/selecting items programmatically/selecting items programmatically.png

The code below provides the solution for this problem.

First, we need to get the list of appropriate master IDs:  

foreach (MasterItem master in db.Items["/sitecore/masters/document"].Masters)

         {

            Response.Write("<br/>");

            Response.Write(master.Name);

         }

The code below selects the masters:  

Database db = Sitecore.Configuration.Factory.GetDatabase("master");

         MasterItem item = (MasterItem)db.Items["/sitecore/masters/document"];

         using(new SecurityDisabler())

         {

            item.InnerItem.BeginEdit();

            item.InnerItem.Fields["__masters"].Value = "{B203C16D-DC37-4EE0-980B-9A1AF68B52AE}|{30235F43-6242-4107-87AB-5267E34EDF0C}";

            item.InnerItem.EndEdit();

         }

Note:   the value “{B203C16D-DC37-4EE0-980B-9A1AF68B52AE}|{30235F43-6242-4107-87AB-5267E34EDF0C}” represents a pipe-separated list of IDs of the masters which should be selected.


Prev Next